package kk.service;

import kk.domain.*;

public class NameListService {
    private Employee[] employees;//用来保存公司所有员工对象

    /**
     * 1.根据项目提供的Data类构建相应大小的employees数组
     * 2.再根据Data类中的数据构建不同的对象，包括Employee、Programmer、Designer和Architect对象，以及相关联的Equipment子类的对象
     * 3.将对象存于数组中
     *
     * @param
     */
    public NameListService() {
        employees = new Employee[Data.EMPLOYEES.length];

        for (int i = 0; i < Data.EMPLOYEES.length; i++) {
            //获取员工类型
            int type = Integer.parseInt(Data.EMPLOYEES[i][0]);
            //获取Employee的四个基本信息
            int id = Integer.parseInt(Data.EMPLOYEES[i][1]);
            String name = Data.EMPLOYEES[i][2];
            int age = Integer.parseInt(Data.EMPLOYEES[i][3]);
            double salary = Double.parseDouble(Data.EMPLOYEES[i][4]);

            Equipment equipment;//设备
            double bonus;//奖金
            int stock;//股票

            switch (type) {
                case Data.EMPLOYEE:
                    employees[i] = new Employee(id, name, age, salary);
                    break;
                case Data.PROGRAMMER:
                    equipment = createEquipment(i);
                    employees[i] = new Programmer(id, name, age, salary, equipment);
                    break;
                case Data.DESIGNER:
                    equipment = createEquipment(i);
                    bonus = Double.parseDouble(Data.EMPLOYEES[i][5]);
                    employees[i] = new Designer(id, name, age, salary, equipment, bonus);
                    break;
                case Data.ARCHITECT:
                    equipment = createEquipment(i);
                    bonus = Double.parseDouble(Data.EMPLOYEES[i][5]);
                    stock = Integer.parseInt(Data.EMPLOYEES[i][6]);
                    employees[i] = new Architect(id, name, age, salary, equipment, bonus, stock);
                    break;
            }
        }
    }

    /**
     * 根绝员工获取设备信息
     *
     * @return
     */
    public Equipment createEquipment(int index) {
        //获取设备基本信息
        int key = Integer.parseInt(Data.EQUIPMENTS[index][0]);
        String modelOrName = Data.EQUIPMENTS[index][1];

        switch (key) {
            case Data.PC:
                String display = Data.EQUIPMENTS[index][2];
                return new PC(modelOrName, display);
            case Data.NOTEBOOK:
                int price = Integer.parseInt(Data.EQUIPMENTS[index][2]);
                return new NoteBook(modelOrName, price);
            case Data.PRINTER:
                String type = Data.EQUIPMENTS[index][2];
                return new Printer(modelOrName, type);
        }

        return null;
    }

    /**
     * 获得公司全部员工数组
     *
     * @return
     */
    public Employee[] getAllEmployees() {
        return employees;
    }

    /**
     * 获取指定id的员工对象
     *
     * @param id
     * @return
     */
    public Employee getEmployee(int id) throws TeamException {
        for (int i = 0; i < employees.length; i++) {
            if (employees[i].getId() == id) {
                return employees[i];
            }
        }

        throw new TeamException("找不到指定的员工");

    }


}
